import {defineStore} from 'pinia'
import { getToken, removeToken, setToken } from '@/utils/auth'

export const useUserStore = defineStore('user', {
  state: () => {
    return {
      token: getToken(),
      userInfo: {},
    }
  },
  getters: {

  },
  actions: {
    login() {
      return new Promise((resolve) => {
        if (!this.token) {
          this.token = 'admin'
          setToken('admin')
          resolve()
        } else {
          console.log('已登录');
          resolve()
        }
      })
    },

    getInfo() {
      return new Promise((resolve) => {
        // 有时候直接用获取到的token请求服务器，服务器会觉得这个token会已过期（可能更准确的说是还没到使用期限？），所以设置一个getinfo的延迟
        setTimeout(() => {
            this.userInfo = {name: test}
            resolve()
        }, 500);
      })
    },


    clearToken() {
      return new Promise((resolve) => {
        removeToken()
        this.token = ''
        resolve()
      })
    },

  }
})